package whisper.service.auth.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import whisper.common.support.jwt.JWTConstant;
import whisper.common.support.response.WebResponse;
import whisper.common.utils.JWTUtils;
import whisper.service.auth.domain.request.UserRequest;
import whisper.support.user.domain.User;
import whisper.support.user.service.UserService;

/**
 * Created by JT on 2018/5/11
 */
@RestController
@RequestMapping(value = "/whisper/auth", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    public WebResponse login(@RequestBody UserRequest userRequest) {
        User user = userService.checkUser(userRequest.getUsername(), userRequest.getPassword());
        return new WebResponse().ok().with(JWTConstant.JWT_TOKEN_FLAG, JWTUtils.sign(user.getUsername(), user.getPassword()));
    }

}
